package com.cmw.auntapp.component;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cmw.auntapp.R;
import com.cmw.auntapp.model.GvlistModel;

import java.util.List;

import androidx.annotation.Nullable;

/**
 * @Desc 自定义Select下拉框
 * @Author chengmingwei
 * @Date 2021-04-14 20:17
 */
public class SelectSpinner extends LinearLayout {

    private View contentBoxView;
    //tv_select_text
    private TextView tvSelectText;

    //bt_select_dropdown
    private ImageView btSelectDropDown;

    private Context mcontext;

    private List<GvlistModel> mItems;
    OnItemSelectedListener listener;
    private SpinnerPopWindow mSpinerPopWindow;
    private SpinnerAdapter mAdapter;

    private String value;


    public SelectSpinner(Context context) {
        super(context);
        this.mcontext = context;
        this.initView();
    }

    public SelectSpinner(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mcontext = context;
        this.initView();
    }


    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(this.mcontext);
        contentBoxView = inflater.inflate(R.layout.select_spinner, null);
        addView(contentBoxView);
        tvSelectText = contentBoxView.findViewById(R.id.tv_select_text);
        btSelectDropDown = contentBoxView.findViewById(R.id.bt_select_dropdown);
        tvSelectText.setOnClickListener(onClickListener);
        btSelectDropDown.setOnClickListener(onClickListener);
    }

    private OnClickListener onClickListener = (view) -> {
        startPopWindow();
    };

    public void setData(List<GvlistModel> datas){
        mItems = datas;
        if (!TextUtils.isEmpty(value)) {
            this.setValue(value);
        }
    }

    public void setOnItemSelectedListener(OnItemSelectedListener listener){
        this.listener = listener;
    }


    public void startPopWindow(){
        mAdapter = new SpinnerAdapter(mcontext);
        mAdapter.refreshData(mItems, 0);

        mSpinerPopWindow = new SpinnerPopWindow(mcontext);
        mSpinerPopWindow.setAdatper(mAdapter);
        mSpinerPopWindow.setItemListener(new OnItemSelectedListener(){
            @Override
            public void onItemSelected(int pos) {
                GvlistModel model = mItems.get(pos);
                Long id = model.getId();
                String name = model.getName();
                Object tag = (null == id) ? model.getCode() : model.getId();
                if (null == tag) tag = name;
                tvSelectText.setText(name);
                tvSelectText.setTag(tag);
                if (listener != null) listener.onItemSelected(pos);
            }
        });
        showSpinWindow();
    }

    public String getText() {
        return null != tvSelectText.getText() ? tvSelectText.getText().toString() : null;
    }

    public String getValue() {
        return null != tvSelectText.getTag() ? tvSelectText.getTag().toString() : tvSelectText.getText().toString();
    }
    private void showSpinWindow(){
        mSpinerPopWindow.setWidth(contentBoxView.getWidth());
        mSpinerPopWindow.showAsDropDown(contentBoxView);
    }

    public void setError(String errMsg) {
        tvSelectText.setError(errMsg);
    }

    public void setValue(String val) {
        this.value = val;
        if (TextUtils.isEmpty(val)) {
            reset();
            return;
        }
        if (null == mItems || mItems.size() == 0) {
            return;
        }
        GvlistModel selGvlist = null;
        for(GvlistModel gvlistModel : mItems){
            Long id = gvlistModel.getId();
            if (val.equals(id.toString())) {
                selGvlist = gvlistModel;
                break;
            }
        }
        if (selGvlist == null) {
            return;
        }
        tvSelectText.setText(selGvlist.getName());
        tvSelectText.setTag(val);
    }

    public void reset() {
        tvSelectText.setText("");
        tvSelectText.setTag(null);
    }

    public interface OnItemSelectedListener {
        void onItemSelected(int pos);
    }

}
